package com.gtlab1207.br_awareness.ctrls;

import com.gtlab1207.br_awareness.common.Result;
import com.gtlab1207.br_awareness.models.domain.SecurityEvent;
import com.gtlab1207.br_awareness.services.LogRecordService;
import com.gtlab1207.br_awareness.services.TotalEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息加密
 */
@Slf4j
@RestController
@RequestMapping("/event/")
public class SecurityEventCtrl extends BaseCtrl{


    @Autowired
    private TotalEventService totalEventService;
    @Autowired
    private LogRecordService logRecordService;

    @PostMapping("/insert")
    public Result saveSecurityEvent(@RequestBody SecurityEvent record) {
        System.out.println(record.toString());
            totalEventService.addSecurityEvent(record);
        return this.send(200,"保存成功");

    }
    //删除 SecurityEvent
    @GetMapping("/delete")
    public Result deleteSecurityEvent(String ip){
        totalEventService.deleteSecurityEvent(ip);
        return this.send(200,"删除成功");
    }
    //更新 SecurityEvent
    @PostMapping("/update")
    public Result updateSecurityEvent(@RequestBody SecurityEvent record){
        System.out.println(record.toString());
        totalEventService.updateSecurityEvent(record);
        return this.send(200,"修改成功");
    }
    
    @PostMapping("/select/list")
    public Result selectAllSecurityEvent(@RequestBody SecurityEvent record){
        System.out.println(record.toString());
        List<SecurityEvent> data=totalEventService.selectAll();
        return this.send(data);
    }
    @PostMapping("/select_pre_seven_day")
    public Result selectPreSevenDay(){
        List<SecurityEvent> data=totalEventService.selectListByState(1);
        return this.send(data);
    }
    @GetMapping("/update_all_ip_score")
    public Result updateAllIpScore(){
        totalEventService.updateAllIpScore();
        return this.send(totalEventService.selectListByState(1));
    }
    //192.168.74.1
    @GetMapping("/update_one_ip_score")
    public Result updateScoreByIp(String ip){
        SecurityEvent event = new SecurityEvent();
        event.setIp(ip);
        event.setState(1);
        List<SecurityEvent> securityEvent = totalEventService.selectSecurityEventByCondition(event);
        System.out.println(securityEvent.size()+"===="+securityEvent);
        securityEvent.get(0).setSecurityEvent(null);
        securityEvent.get(0).setState(null);
        securityEvent.get(0).setScore(100.0);
        totalEventService.updateSecurityEvent(securityEvent.get(0));
        return this.send(totalEventService.selectSecurityEventByCondition(event));
    }
    @GetMapping("/select_in_seven_day")
    public Result selectEventInSevenDay(){
        Map<String,Object> result=new HashMap<>();
        result.put("data",totalEventService.selectEventInSevenDay());
        return this.send(result);
    }
    @GetMapping("/select_every_day")
    public Result selectEventScoreEveryDay(){
        Map<String,Object> result=new HashMap<>();
        result.put("data",totalEventService.selectEventScoreEveryDay());
        return this.send(result);
    }

}
